import time

import allure
from selenium.webdriver.common.by import By
from pytest00.tpshop.util import Driver
class Admin_login:
    @classmethod
    @allure.step("调用后台登录")
    def admin_login(self):
        driver=Driver.get_driver()
        driver.get("http://localhost/index.php/Admin/Admin/login.html")
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,'[name="username"]').send_keys('admin')
        driver.find_element(By.CSS_SELECTOR, '[name="password"]').send_keys('123456')
        driver.find_element(By.CSS_SELECTOR, '[name="vertify"]').send_keys('8888')
        allure.attach(driver.get_screenshot_as_png(), "后台登录截图", allure.attachment_type.PNG)
        driver.find_element(By.CSS_SELECTOR, '[type="button"]').click()

class Step:
    @classmethod
    @allure.step("调用点击后台系统")
    def click_sys(self):
        driver = Driver.get_driver()
        driver.find_element(By.CSS_SELECTOR, '[data-param="system"] a').click()
    @classmethod
    @allure.step("调用点击后台角色列表")
    def click_roul(self):
        driver = Driver.get_driver()
        driver.find_element(By.CSS_SELECTOR, '[class="ico-system-4"]').click()
class Act:
    @classmethod
    @allure.step("调用后台新建角色")
    def new_data(self,name1,name2,rou):
        driver = Driver.get_driver()
        driver.switch_to.frame(driver.find_element(By.ID, "workspace"))
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, '[title="添加管理员"] span i').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, '[id="user_name"]').send_keys(name1)
        driver.find_element(By.CSS_SELECTOR, '[id="email"]').send_keys(name2)
        driver.find_element(By.CSS_SELECTOR, '[id="password"]').send_keys('123456')
        driver.find_element(By.CSS_SELECTOR, '[name="role_id"]').click()
        driver.find_element(By.CSS_SELECTOR, '[value="%d"]'%rou).click()
        allure.attach(driver.get_screenshot_as_png(), "后台登新建数据截图", allure.attachment_type.PNG)
        driver.find_element(By.CSS_SELECTOR, '[id="submitBtn"]').click()
    @classmethod
    @allure.step("调用后台验证角色")
    def check_data(cls,n):
        driver = Driver.get_driver()
        driver.switch_to.frame(driver.find_element(By.ID, "workspace"))
        time.sleep(1)
        name1=driver.find_element(By.XPATH, '//tbody/tr[%d]/td[3]/div'%n).text
        roe = driver.find_element(By.XPATH, '//tbody/tr[%d]/td[4]/div'%n).text
        name2 = driver.find_element(By.XPATH, '//tbody/tr[%d]/td[5]/div'%n).text
        t = driver.find_element(By.XPATH, '//tbody/tr[%d]/td[6]/div'%n).text
        allure.attach(driver.get_screenshot_as_png(), "后台验证数据截图", allure.attachment_type.PNG)
        return name1,name2,roe,t
    @classmethod
    @allure.step("调用后台删除角色")
    def del_data(cls,name1):
        driver = Driver.get_driver()
        driver.refresh()
        driver.switch_to.frame(driver.find_element(By.ID, "workspace"))
        driver.find_element(By.CSS_SELECTOR, '[name="keywords"]').send_keys(name1)
        driver.find_element(By.CSS_SELECTOR, '[type="submit"]').click()
        time.sleep(1)
        name2= driver.find_element(By.XPATH, '//tbody/tr[1]/td[3]/div').text
        assert name2==name1
        driver.find_element(By.XPATH, '//tbody/tr[1]/td[7]/div/a[2]').click()
        allure.attach(driver.get_screenshot_as_png(), "后台删除数据截图", allure.attachment_type.PNG)
        driver.find_element(By.CSS_SELECTOR, '[class="layui-layer-btn0"]').click()
def rol_data(n):
    if n==1:
        return '超级管理员'
    elif n==2:
        return '编辑'
    elif n==4:
        return '客服'
    else:
        return '仓管员'
#









##获取列表数据总数
""""
Admin_login().admin_login()
Step.click_sys()
Step.click_roul()
driver = Driver.get_driver()
time.sleep(1)
driver.switch_to.frame(driver.find_element(By.ID, "workspace"))
time.sleep(1)
tab=driver.find_element(By.CSS_SELECTOR, '[id="flexigrid"] table tbody')

all_rows = tab.find_elements(By.TAG_NAME, "tr")
n = len(all_rows)
print(n)
time.sleep(10000)


"""

















